# coding:utf8

from appserver import db_store
from sqlalchemy.orm import relationship, backref
from sqlalchemy import func,or_
import datetime
from models.entity.model_base import ModelBase

"""
设备表
"""


class DeviceInfo(ModelBase):
    __tablename__ = "device_info"
    __bind_key__ = "main"

    # autoincrement=True,
    id = db_store.Column(db_store.Integer, primary_key=True, comment=u'主键id')
    weight = db_store.Column(db_store.String(16), comment=u"设备重量")
    device_model = db_store.Column(db_store.String(20), comment=u'设备型号')
    temperature = db_store.Column(db_store.String(20), comment=u'环境温度')
    pump_num = db_store.Column(db_store.Integer, comment=u'水泵数量')
    pump_type = db_store.Column(db_store.String(40),  comment=u'水泵数量')
    rated_water = db_store.Column(db_store.String(16),  comment=u'额定供水流量')
    control_case_type = db_store.Column(db_store.String(40),  comment=u'控制柜型号')
    power = db_store.Column(db_store.String(40),  comment=u'额定功率')
    voltage = db_store.Column(db_store.String(40),  comment=u'电压')
    steady_flow_type = db_store.Column(db_store.String(40),  comment=u'稳流罐规格')
    water_distance=db_store.Column(db_store.String(40),  comment=u'额定供水扬程')
    out_size = db_store.Column(db_store.String(100),  comment=u'设备外形尺寸')
    all_voltage = db_store.Column(db_store.String(100),  comment=u'设备总功率')
    flow_controler= db_store.Column(db_store.String(100),  comment=u'流量控制器')
    enhance_pressure = db_store.Column(db_store.String(100),  comment=u'增压装置规格')
    gas_enhance_pressure = db_store.Column(db_store.String(100),  comment=u'气体增压规格')
    supplement_device = db_store.Column(db_store.String(100),  comment=u'补偿器规格')
    water_case =  db_store.Column(db_store.String(100),  comment=u'水箱规格')
    product_date = db_store.Column(db_store.Date,  comment=u'出场时间',default = datetime.datetime.utcnow())
    device_id = db_store.Column(db_store.Integer, db_store.ForeignKey('devices.id'), nullable=False,
                                     comment=u"设备ID",unique = True)
    # device = relationship("device_info", backref=backref('Device', order_by=id))

    def __repr__(self):
        return "<DeviceInfo %s>" % self.device_id

    def to_dict(self):
        return {"id": +self.id, "device_id": self.device_id, "device_model": self.device_model,
         "temperature": self.temperature,
         "pump_num": self.pump_num, "pump_type": self.pump_type, "rated_water": self.rated_water,
         "voltage": self.voltage, "power": self.power,
         "control_case_type": self.control_case_type, "steady_flow_type": self.steady_flow_type,
         "water_distance": self.water_distance, "out_size": self.out_size,
         "all_voltage": self.all_voltage, "flow_controler": self.flow_controler,
         "enhance_pressure": self.enhance_pressure, "gas_enhance_pressure": self.gas_enhance_pressure,
         "supplement_device": self.supplement_device, "water_case": self.water_case,
         "weight": self.weight,
         "product_date": self.product_date.strftime("%F")}

    @staticmethod
    def get_device_info_all():
        return DeviceInfo.query.all()

    @staticmethod
    def get_device_info_by_id(id_num):
        return DeviceInfo.query.filter_by(id=id_num).first()

    @staticmethod
    def get_info_by_device_id(id):
        return DeviceInfo.query.filter_by(device_id=id).first()

    @staticmethod
    def get_device_by_type_id(parent_id):
        return DeviceInfo.query.filter_by(device_type_id=parent_id).first()

    @staticmethod
    def search(query_word):
        data = []
        buttons = DeviceInfo.query.filter(
            or_(DeviceInfo.device_model.like('%' + query_word + '%'), DeviceInfo.flow_controler.like('%' + query_word + '%'),
                DeviceInfo.steady_flow_type.like('%' + query_word + '%'))).all()
        for obj in buttons:
            data.append(obj.to_dict())
        return data